/*
 * Copyright (C) 2004, Velocityme Partnership
 * GroupAccessActions.java
 *
 * Created on 15 September 2004, 04:03
 */

package com.velocityme.www.interfaces;

import com.velocityme.session.GroupAccessLocal;
import com.velocityme.session.KeyLocal;
import com.velocityme.www.actionforms.GroupAccessActionForm;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author  Robert
 */
public class GroupAccessActions extends DispatchAction {
    
    /** Creates a new instance of GroupAccessActions */
    public GroupAccessActions() {
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        GroupAccessLocal groupAccessLocal = lookupGroupAccessBean();
        Long nodeId = (Long)session.getAttribute("nodeId");
        if (request.getParameter("btnSubmit").equals("OK")) {
            Long groupId = ((GroupAccessActionForm)form).getGroupId();
            Long roleId = ((GroupAccessActionForm)form).getRoleId();
            groupAccessLocal.createGroupAccess(key, nodeId, groupId, roleId);
        }
        // Cancel simply displays the original node again
        else {
        }
        request.setAttribute("nodeId", nodeId);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
    public ActionForward deleteCheck(ActionMapping mapping, 
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        session.setAttribute("groupAccessValue", form);
        return mapping.findForward("ACTION_DELETE");
    }
    
    public ActionForward delete(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        if (request.getParameter("btnSubmit").equals("OK")) {
            KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
            GroupAccessLocal groupAccessLocal = lookupGroupAccessBean();
            Long groupAccessId = ((GroupAccessActionForm)form).getAccessId();
            groupAccessLocal.deleteGroupAccess(key, groupAccessId);
        }
        request.setAttribute("nodeId", session.getAttribute("nodeId"));
        return mapping.findForward("ACTION_SUCCESS");
    }

    private GroupAccessLocal lookupGroupAccessBean() {
        try {
            Context c = new InitialContext();
            return (GroupAccessLocal) c.lookup("java:comp/env/GroupAccessBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    
}
